<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <a-row>
        <a-col :span="6">
          <a-button v-action:add type="primary" icon="plus" @click="handleAdd">新增</a-button>
        </a-col>
      </a-row>
    </div>
    <s-table ref="table" :scroll="{x:1500}" rowKey="referee_id" :loading="isLoading" :columns="columns" :data="loadData" :pageSize="15">
      <div slot="certificate" slot-scope="text, item">
        <a-tooltip placement="right" arrow-point-at-center>
          <div slot="title">
            {{ text }}
          </div>
          <div class="certificate">{{ text }}</div>
        </a-tooltip>
      </div>
      <div slot="position" slot-scope="text, item">
        <a-tooltip placement="right" arrow-point-at-center>
          <div slot="title">
            {{ text }}
          </div>
          <div class="position">{{ text }}</div>
        </a-tooltip>
      </div>
      <div class="actions" slot="action" slot-scope="text, item">
        <a v-action:edit @click="handleEdit(item)">编辑</a>
        <template>
          <a v-action:delete @click="handleDelete(item)">删除</a>
        </template>
      </div>
    </s-table>
    <AddForm ref="AddForm" @handleSubmit="handleRefresh" />
    <EditForm ref="EditForm" @handleSubmit="handleRefresh" />
  </a-card>
</template>

<script>
import * as Api from '@/api/arena/referee'
import { STable } from '@/components'
import AddForm from './referee/AddForm.vue'
import EditForm from './referee/EditForm.vue'

export default {
  name: 'RefereeList',
  components: {
    STable,
    AddForm,
    EditForm,
  },
  data() {
    return {
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: '裁判ID',
          dataIndex: 'referee_id',
          align: 'center',
          width: '80px'
        },
        {
          title: '姓名',
          dataIndex: 'referee_name',
          scopedSlots: { customRender: 'referee_name' },
          align: 'center',
        },
        {
          title: '工作岗位',
          dataIndex: 'post',
          align: 'center', 
          width: '200px'
        },
        {
          title: '职位',
          dataIndex: 'position',
          scopedSlots: { customRender: 'position' },
          align: 'center',
        },
        {
          title: '证书',
          dataIndex: 'certificate',
          scopedSlots: { customRender: 'certificate' },
          align: 'center',
        },
        {
          title: '价格',
          dataIndex: 'price',
          align: 'center',
          width: '100px'
        },
        {
          title: '排序',
          dataIndex: 'sort',
          align: 'center',
          width: '50px'
        },
        {
          title: '添加时间',
          dataIndex: 'create_time',
          align: 'center',
          width: '150px', 
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          scopedSlots: { customRender: 'action' },
          align: 'center',
        },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (param) => {
        return Api.list({ ...param, ...this.queryParam }).then((response) => {
          return response.data
        })
      },
    }
  },
  computed: {
    statusText() {
      return (status) => {
        const text = ['开启', '关闭']
        return text[status]
      }
    },
  },
  created() {},
  methods: {
    /**
     * 新增记录
     */
    handleAdd() {
      this.$refs.AddForm.add()
    },

    /**
     * 编辑记录
     */
    handleEdit(item) {
      this.$refs.EditForm.edit(item)
    },

    /**
     * 删除记录
     */
    handleDelete(item) {
      const app = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk() {
          return Api.del({refereeId: item.referee_id})
            .then((result) => {
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally((result) => {
              modal.destroy()
            })
        },
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh(bool = false) {
      this.$refs.table.refresh(bool)
    },

    /**
     * 检索查询
     */
    onSearch() {
      this.handleRefresh(true)
    },

  },
}
</script>

<style lang="less" scoped>
.position,
.certificate {
  width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
